/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.gui;

import com.logical.allhit.setting.Account;
import com.logical.allhit.setting.Setting;
import com.logical.allhit.util.ScrollUtil;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author comp18
 */
public class AccountList extends javax.swing.JDialog {

    /**
     * Creates new form ProxyList
     */
    public AccountList(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        accountTable.setModel(new AccountTableModel());
        accountTable.setAutoCreateRowSorter(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        accountTable = new javax.swing.JTable();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        firstButton = new javax.swing.JButton();
        upButton = new javax.swing.JButton();
        downButton = new javax.swing.JButton();
        lastButton = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Account List");

        accountTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(accountTable);

        addButton.setText("Add Account");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        removeButton.setText("Remove");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        editButton.setText("Edit");
        editButton.setToolTipText("");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        firstButton.setText("First");
        firstButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                firstButtonActionPerformed(evt);
            }
        });

        upButton.setText("UP");
        upButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                upButtonActionPerformed(evt);
            }
        });

        downButton.setText("Down");
        downButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downButtonActionPerformed(evt);
            }
        });

        lastButton.setText("Last");
        lastButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lastButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(addButton, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                    .addComponent(removeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(editButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(firstButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(upButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(downButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lastButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(49, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(firstButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(upButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(downButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lastButton)
                        .addGap(0, 17, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        try {
            Account account = new Account();
            new AddAccount(account, MainView.getInstance().getFram(), true).setVisible(true);
            ScrollUtil.scroll(accountTable, ScrollUtil.LEFT, ScrollUtil.BOTTOM);
            AccountTableModel model = (AccountTableModel) accountTable.getModel();
            model.addAccount(account);
        } catch (Exception e) {
            Logger.getLogger(AccountList.class.getName()).log(Level.WARNING, e.getMessage(), e);
        }
    }//GEN-LAST:event_addButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        AccountTableModel model = (AccountTableModel) accountTable.getModel();
        int[] selected = accountTable.getSelectedRows();
        for (int i : selected) {
            model.remove(i);
        }
        ScrollUtil.scroll(accountTable, ScrollUtil.LEFT, ScrollUtil.BOTTOM);
    }//GEN-LAST:event_removeButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
        try {
            AccountTableModel model = (AccountTableModel) accountTable.getModel();
            int selected = accountTable.getSelectedRow();
            new AddAccount(model.getAccountAt(selected), MainView.getInstance().getFram(), true).setVisible(true);
            model.fireTableDataChanged();
            Setting.getInstance().fireSettingChandeEvent();
        } catch (Exception e) {
            Logger.getLogger(AccountList.class.getName()).log(Level.WARNING, e.getMessage(), e);
        }
    }//GEN-LAST:event_editButtonActionPerformed

    private void firstButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_firstButtonActionPerformed
        AccountTableModel model = (AccountTableModel) accountTable.getModel();
        int selected = accountTable.getSelectedRow();
        model.moveToFirst(model.getAccountAt(selected));
        accountTable.changeSelection(0, 0, false, false);
    }//GEN-LAST:event_firstButtonActionPerformed

    private void upButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButtonActionPerformed
        AccountTableModel model = (AccountTableModel) accountTable.getModel();
        int selected = accountTable.getSelectedRow();
        model.moveUP(model.getAccountAt(selected));
        accountTable.changeSelection(--selected, 0, false, false);
    }//GEN-LAST:event_upButtonActionPerformed

    private void downButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downButtonActionPerformed
        AccountTableModel model = (AccountTableModel) accountTable.getModel();
        int selected = accountTable.getSelectedRow();
        model.moveDown(model.getAccountAt(selected));
        accountTable.changeSelection(++selected, 0, false, false);
    }//GEN-LAST:event_downButtonActionPerformed

    private void lastButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lastButtonActionPerformed
        AccountTableModel model = (AccountTableModel) accountTable.getModel();
        int selected = accountTable.getSelectedRow();
        model.moveToLast(model.getAccountAt(selected));
        accountTable.changeSelection(model.getRowCount()-1, 0, false, false);
    }//GEN-LAST:event_lastButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable accountTable;
    private javax.swing.JButton addButton;
    private javax.swing.JButton downButton;
    private javax.swing.JButton editButton;
    private javax.swing.JButton firstButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton lastButton;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton upButton;
    // End of variables declaration//GEN-END:variables

    static class AccountTableModel extends AbstractTableModel {

        ArrayList<Account> accounts;
        // These are the names of the columns represented by this TableModel
        static final String[] columnNames = new String[]{"Name", "UID"};
        // These are the types of the columns represented by this TableModel
        static final Class[] columnTypes = new Class[]{String.class, Long.class};

        public AccountTableModel() {
            accounts = Setting.getInstance().getAccounts();
        }

        @Override
        public int getRowCount() {
            return accounts.size();
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }

        @Override
        public Class getColumnClass(int column) {
            return columnTypes[column];
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Account account = accounts.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    return account.getName();
                case 1:
                    return account.getUid();
                default:
                    return new Object();
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        public boolean remove(int i) {
            accounts.remove(i);
            fireTableDataChanged();
            return true;
        }

        public Account getAccountAt(int i) {
            return accounts.get(i);
        }

        public void addAccount(Account account) {
            accounts.add(account);
            fireTableDataChanged();
        }
        
        public void moveToFirst(Account account){
            int index = accounts.indexOf(account);
            if(index > 0){
                accounts.remove(account);
                accounts.add(0, account);
            }
            fireTableDataChanged();
        }
        
        public void moveToLast(Account account){
            int index = accounts.indexOf(account);
            if(index > -1 && index < (accounts.size()-1)){
                accounts.remove(account);
                accounts.add(accounts.size(), account);
            }
            fireTableDataChanged();
        }
        
        public void moveUP(Account account){
            int index = accounts.indexOf(account);
            if(index > -1 ){
                accounts.remove(account);
                accounts.add(--index, account);
            }
            fireTableDataChanged();
        }
        
        public void moveDown(Account account){
            int index = accounts.indexOf(account);
            if(index > -1){
                accounts.remove(account);
                accounts.add(++index, account);
            }
            fireTableDataChanged();
        }
        
    }
}
